package com.example.service

import com.example.controller.request.UserRequest
import com.example.model.domain.UserProfile
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.random.Random

/**
 *
 *
 * @author wangli
 * @date 2025-10-10
 */
@SpringBootTest
class UserServiceTest {

    @Autowired
    lateinit var userService: UserService

    @Autowired
    lateinit var userAsyncService: UserAsyncService

    lateinit var userReq: UserRequest

    @BeforeEach
    fun setUp() {
        userReq = UserRequest(
            username = "test user" + Random.nextInt(1, 100000),
            password = "password123",
            email = "email@google.com" + Random.nextInt(1, 100000),
            userProfile = UserProfile(
                id = null,
                userId = null,
                firstName = "Test",
                lastName = "User",
                phoneNumber = "1234567890",
                address = "123 Test St"
            )
        )
    }

    @Test
    fun createUser_with_transaction() {
        val userId = userService.createUserWithTransaction(userReq)
        assertNotNull(userId)
    }

    @Test
    fun createUser_with_transaction_rollback_exception() {
        val userId = userService.createUserWithTransactionRollbackException(userReq)
        assertNotNull(userId)
    }

    @Test
    fun createUser_with_transaction_async() {
        userAsyncService.createUserWithTransactionAsync(userReq)
    }
}